<?php


class AddAdvController {

    public function index() {
        include '../icloud_files/models/AdvModel.php';
        $adv = new AdvModel();

        //if active search
        if($_SERVER['REQUEST_METHOD'] == 'GET' && count($_GET) > 1) {
            $_SESSION['last_search'] = $_GET;
            $adv->search_adv($_SESSION['last_search'], false, false);

            if(count($adv->messages) < 1) {
                //render search page
                $adv->page_title = 'Search Auto Results';
                $adv->get_currencies();
                View::get_instance()->render('adv_search', $adv);
                exit;
            }
        }

        $adv->page_title = 'Ahimeh Autos Home';

        //get recently added and most favored items data
        $adv->recently_added('advertises_cars', 'date_added');
        $adv->most_viewed('advertises_cars', 'views');

        $adv->get_adv_lists('vehicle_states');
        $adv->get_adv_lists('car_types');
        $adv->get_adv_lists('colors');
        $adv->get_adv_lists('makes');

        //echo "<pre>" . print_r($adv, true) . "</pre>";

        View::get_instance()->render('index', $adv);

    }
/* ------------------------------------------------------------------------------------------------- */


    public function index_parts() {
        include '../icloud_files/models/AdvModel.php';
        $adv = new AdvModel();

        //if active search
        if($_SERVER['REQUEST_METHOD'] == 'GET' && count($_GET) > 1) {
            $_SESSION['last_search'] = $_GET;
            $adv->search_adv_parts($_SESSION['last_search'], false, false);
            if(count($adv->messages) < 1) {
                //render search page
                $adv->page_title = 'Search Car Parts Results';
                $adv->get_currencies();
                View::get_instance()->render('adv_search', $adv);
                exit;
            }
        }

        $adv->page_title = 'Ahimeh Car Parts Home';

        //get recently added and most favored items data
        $adv->recently_added('advertises_parts', 'date_added');
        $adv->most_viewed('advertises_parts', 'views');

        $adv->get_adv_lists('vehicle_states');
        $adv->get_adv_lists('car_types');
        $adv->get_adv_lists('colors');
        $adv->get_adv_lists('makes');

        //echo "<pre>" . print_r($adv, true) . "</pre>";

        View::get_instance()->render('index_parts', $adv);

    }
/* ------------------------------------------------------------------------------------------------- */


    public function adv_add() {
        if($_SESSION['is_logged'] !== true) {
            header('Location: ?p=login');
            exit;
        }

        include '../icloud_files/models/AdvModel.php';
        $adv = new AdvModel();
        $adv->page_title = 'Add Stock';

        if($_POST) {
            $adv->require_payments = false; //paypal payments currently inactive
            $adv->check_adv($_POST);

            if(count($adv->messages) <= 0) {
                //process request and add to database -> via model
                $adv->add_adv($_FILES);
                if(isset($adv->success) && $adv->success) {
                    header('Location: ?p=adv_manage');
                    exit;
                }
            }
        }

        $adv->get_adv_lists('vehicle_states');
        $adv->get_adv_lists('car_types');
        $adv->get_adv_lists('colors');
        $adv->get_adv_lists('makes');

        View::get_instance()->render('adv_add', $adv);
    }
/* ------------------------------------------------------------------------------------------------- */


    public function adv_edit() {
        if($_SESSION['is_logged'] !== true) {
            header('Location: ?');
            exit;
        }

        include '../icloud_files/models/AdvModel.php';
        $adv = new AdvModel();
        $adv->page_title = 'Edit Advertise';

        //check if adv_id is correct number
        $adv->fetch_adv($_GET['adv_id'], $_SESSION['user_data']['user_id']);
        if(count($adv->messages) > 0) {
            header('Location: ?');
            exit;
        }

        if($_POST) {
            $adv->check_adv($_POST);

            if(count($adv->messages) <= 0) {
                //process request and add to database -> via model
                $adv->edit_adv($_FILES);
            }
        }


        $adv->get_adv_lists('vehicle_states');
        $adv->get_adv_lists('car_types');
        $adv->get_adv_lists('colors');
        $adv->get_adv_lists('makes');

        //echo "<pre>" . print_r($adv, true) . "</pre>";
        View::get_instance()->render('adv_add', $adv);

    }
/* ------------------------------------------------------------------------------------------------- */


    public function adv_view() {

        if(isset($_GET['type']) && isset($_GET['adv_id'])) {
            include '../icloud_files/models/AdvModel.php';
            $adv = new AdvModel();
            $adv->page_title = 'View Advertise';

            if(htmlspecialchars($_GET['type']) == 'part') {
                $table = "advertises_parts";
                $template = 'adv_view_parts';
            } else {
                $table = "advertises_cars";
                $template = 'adv_view';
            }

            $adv->fetch_adv($_GET['adv_id'], 1, $table);
            if(count($adv->messages) < 1) {
                //increase adv view hits by 1
                $adv->increment_views($_GET['adv_id'], $table);

                //get seller data
                include '../icloud_files/models/UserModel.php';
                $user = new UserModel();

                $user->get_seller($adv->seller_id);
                $adv->seller_data = $user->user_data;
                $adv->messages = $user->messages;

                //check for sent message
                if($_SERVER['REQUEST_METHOD'] == 'POST') {
                    $adv->prepare_adv_inquiry($_POST);

                    if(count($adv->messages) <= 0) {
                    //$user->send_email($email, $title, $body, $headers = '');
                        $user->send_email($adv->email_data[0], $adv->email_data[1],
                            $adv->email_data[2], $adv->email_data[3]);
                        $adv->messages = $user->messages;
                    }
                }
            } else {
                header('Location: ?');
                exit;
            }
        } else {
            header('Location: ?');
            exit;
        }


        View::get_instance()->render($template, $adv);
    }
/* ------------------------------------------------------------------------------------------------- */


    public function adv_manage() {
        if($_SESSION['is_logged'] !== true) {
            header('Location: ?p=login');
            exit;
        }

        include '../icloud_files/models/AdvModel.php';
        $adv = new AdvModel();
        $adv->page_title = 'Manage Your Stocks';
        $adv->get_currencies();

        if(isset($_GET['type']) && isset($_GET['rm_id'])) {
            $adv->remove_adv($_SESSION['user_data']['user_id'], $_GET['type'], $_GET['rm_id']);
            if(!isset($adv->error)) {
                $adv->delete_pics(stripslashes($_GET['rm_id']), $_SESSION['user_data']['user_id'], stripslashes($_GET['type']));
            }
        }

        //fetch all user adv - happens via AJAX , see Class Pagination

        //echo "<pre>" . print_r($adv, true) . "</pre>";
        View::get_instance()->render('adv_manage', $adv);
    }
/* ------------------------------------------------------------------------------------------------- */


    public function adv_add_parts() {
        if($_SESSION['is_logged'] !== true) {
            header('Location: ?p=login');
            exit;
        }

        include '../icloud_files/models/AdvModel.php';
        $adv = new AdvModel();
        $adv->page_title = 'Add Parts Ad';

        if($_POST) {
            $adv->check_adv($_POST, 'parts');
            if(count($adv->messages) <= 0) {
                //process request and add to database -> via model
                $adv->add_adv_parts($_FILES);
                if(isset($adv->success) && $adv->success) {
                    header('Location: ?p=adv_manage');
                    exit;
                }
            }
        }

        $adv->get_adv_lists('makes');

        View::get_instance()->render('adv_add_parts', $adv);
    }
/* ------------------------------------------------------------------------------------------------- */


    public function adv_edit_parts() {
        if($_SESSION['is_logged'] !== true) {
            header('Location: ?');
            exit;
        }

        include '../icloud_files/models/AdvModel.php';
        $adv = new AdvModel();
        $adv->page_title = 'Edit Parts Ad';

        //check if adv_id is correct number
        $adv->fetch_adv($_GET['adv_id'], $_SESSION['user_data']['user_id'], 'advertises_parts');
        if(count($adv->messages) > 0) {
            header('Location: ?');
            exit;
        }

        if($_POST) {
            $adv->check_adv($_POST, 'parts');

            if(count($adv->messages) <= 0) {
                //process request and add to database -> via model
                $adv->edit_adv_parts($_FILES);
            }
        }


        $adv->get_adv_lists('makes');

        //echo "<pre>" . print_r($adv, true) . "</pre>";
        View::get_instance()->render('adv_add_parts', $adv);

    }
/* ------------------------------------------------------------------------------------------------- */

}